1 module hip.gui.label;
2 public import hip.gui.widget;
3 public import hip.api.graphics.text;
4 public import hip.api.graphics.color;
5 import hip.game2d.text;
6 
7 class Label : Widget
8 {
9     public HipText txtDraw;
10     protected IWidgetRenderer bkgRenderer;
11 
12     this(string text)
13     {
14         this.txtDraw = new HipText();
15         this.txtDraw.wordWrap = true;
16         this.text = text;
17         setAlign(HipTextAlign.centerLeft);
18     }
19     public Label setColor(HipColor color)
20     {
21         this.txtDraw.color = color;
22         return this;
23     }
24 
25     public void setBackgroundRenderer(HipColor bkgColor )
26     {
27         this.bkgRenderer = new DebugWidgetRenderer(bkgColor);
28     }
29 
30     public void setBackgroundRenderer(IWidgetRenderer bkgRenderer)
31     {
32         this.bkgRenderer = bkgRenderer;
33     }
34     public string text(string text)
35     {
36         txtDraw.text = text;
37         txtDraw.getSize(this.width, this.height);
38         return text;
39     }
40 
41     public string text(){return txtDraw.text;}
42 
43     public bool wordWrap(){return txtDraw.wordWrap;}
44     public bool wordWrap(bool bWordWrap){return txtDraw.wordWrap = bWordWrap;}
45 
46     public Label setAlign(HipTextAlign align_)
47     {
48         txtDraw.setAlign(align_);
49         txtDraw.getSize(this.width, this.height);
50         return this;
51     }
52 
53     public Label setSize(int width, int height)
54     {
55         txtDraw.bounds = Size(width, height);
56         txtDraw.getSize(this.width, this.height);
57         return this;
58     }
59     private void getTextPosition(out int x, out int y)
60     {
61         getPositionFromAlignment(
62             worldTransform.x, worldTransform.y, 
63             txtDraw.width,  txtDraw.height, 
64             txtDraw.align_,
65             x, y, 
66             txtDraw.bounds
67         );
68     }
69 
70     override void onRender()
71     {
72         int bkgX = void, bkgY = void;
73         getTextPosition(bkgX, bkgY);
74         txtDraw.setPosition(worldTransform.x, worldTransform.y);
75         if(bkgRenderer !is null) bkgRenderer.render(bkgX, bkgY, txtDraw.width, txtDraw.height);
76         txtDraw.draw();
77     }
78 }